package cn.tl.user.service.impl;

import cn.tl.base.exception.BusinessException;
import cn.tl.base.service.impl.BaseServiceImpl;
import cn.tl.user.domain.LoginInfo;
import cn.tl.user.domain.User;
import cn.tl.user.domain.dto.UserDto;
import cn.tl.user.mapper.LoginInfoMapper;
import cn.tl.user.mapper.UserMapper;
import cn.tl.user.service.IUserService;
import cn.tl.utils.MD5Utils;
import com.google.common.base.Verify;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;


@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LoginInfoMapper loginInfoMapper;
    @Override
    public User findByPhone(String phone) {
        return userMapper.findByPhone(phone);
    }

    @Override
    @Transactional
    public void register(UserDto userDto) throws BusinessException {

        VerifyData(userDto);
        //把userDto转变为user对象
        User user = userDto2User(userDto);
        //把user对象转化为loginInfo
        LoginInfo loginInfo = user2LoginInfo(user);
        //存储对象
        loginInfoMapper.save(loginInfo);
        user.setLogininfo_id(loginInfo.getId());
        userMapper.save(user);

    }


    private LoginInfo user2LoginInfo(User user) {
        LoginInfo info = new LoginInfo();
        BeanUtils.copyProperties(user,info);
        //单独处理的字段
        info.setType(1);
        info.setDisable(user.getState());
        return info;

    }

    private User userDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername("p_" + userDto.getPhone());
        user.setPhone(userDto.getPhone());
        user.setState(1);
        user.setCreatetime(new Date());
        //加密
        String salt = UUID.randomUUID().toString();
        MD5Utils.encrypByMd5(userDto.getPassword()+salt);
        user.setSalt("");
        user.setPassword(MD5Utils.encrypByMd5(userDto.getPassword()+salt));

        return user;
    }

    private void VerifyData(UserDto userDto) throws BusinessException {
        //手机号不为空
        if(StringUtils.isNullOrEmpty(userDto.getPhone())){
            throw new BusinessException("手机号不能为空");
        }
        if(StringUtils.isNullOrEmpty(userDto.getCode())){
            throw new BusinessException("验证码不能为空");
        }
        if(StringUtils.isNullOrEmpty(userDto.getPassword())){
            throw new BusinessException("密码不能为空");
        }
        //验证验证码是否正确
        String codeInredis = (String) redisTemplate.opsForValue().get(userDto.getPhone()+":"+userDto.getCodeType());
        //取验证码
        if(codeInredis==null){
            throw new BusinessException("验证码无效");
        };
        if (!codeInredis.split(":")[0].equals(userDto.getCode())){
            throw new BusinessException("验证码错误");
        }
    }


}
